<?php

namespace dk\widget;
use wm\helper\AssetCache;

class JsCache extends Js {
    protected $cache = true;
    protected $nocacheFiles = [];

    function appendFiles($files, $cache = true){
        if($cache){
            $this->files += $files;
        }else{
            $this->nocacheFiles += $files;
        }
        return null;
    }

    function printJSFile($files = null){
        if($files){
            $this->appendFiles($files);
        }
        if(!$this->files && !$this->nocacheFiles){
            return null;
        }
        
        $files = $this->nocacheFiles;
        if($this->files){
            
            foreach ($this->files as $k => $v) {
                if(strpos($v, 'http') !== false){
                    $files[$k] = $v;
                    unset($this->files[$k]);
                }
            }

            if(count($this->files) > 2){
                $files[] = AssetCache::getResouce('js', $this->files);
            } else {
                $files += $this->files;
            }
        }
        
        $this->files = $files;
        return $this->jsFileHtml($files);
    }
}